<?php

namespace Index\Controller;
use Think\Controller;
use Index\ViewModel;
use Index\Service;

class ReportController extends CommonController {

    public function index() {
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $map['chat_complaint.app_flag'] = $app_code;
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }

        if ($keyword) {
            if (is_numeric($keyword) && preg_match("/^1[34578]\d{9}$/ ", $keyword)) {
                $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
            } else if (is_numeric($keyword)) {
                $where['chat_complaint.id'] = array('eq', $keyword);
            } else if (is_string($keyword)) {
                $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
            }
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\ReportViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $field = array(
            'ctime', 'id', 'complaint_type_name', 'user_logo', 'user_id', 'user_phone', 'user_name', 'user_phone', 'user_sex',
            'complaint_user_id', 'complaint_logo', 'complaint_nickname', 'complaint_sex', 'complaint_phone', 'pic'
        );
        $data = $viewModel->limit($page->listfirst, $page->listRows)->field($field)->order('ctime desc')->where($map)->select();
//        echo $data;
//        die;
        $sex = [1 => '男', 2 => '女', 3 => '未知'];
        foreach ($data as $key => $value) {
            $data[$key]['imgs'] = empty($value['pic']) ? '' : explode(",", $value['pic']);

            $data[$key]['img'] = empty($data[$key]['img']) ? '' : explode(',', $data[$key]['img'])[0];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
            $user_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />手机号码：' . $data[$key]['user_phone'] . '<br />性别：' . $sex[$data[$key]['user_sex']];
            $data[$key]['user_info'] = $user_info;
            //被举报用户信息拼接
            $complaint_userinfo = $this->app_name2 . '号：' . $data[$key]['complaint_user_id'] . '<br />用户昵称：' . $data[$key]['complaint_nickname'] . '<br />手机号码：' . $data[$key]['complaint_phone'] . '<br />性别：' . $sex[$data[$key]['complaint_sex']];
            $data[$key]['user_info'] = $user_info;
            $data[$key]["complaint_userinfo"] = $complaint_userinfo;
        }
        $search = array('keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search
        ));
        $this->display();
    }
//举报详情
    public function reporttail() {
        $sex_enum = getenumbykey('sex');
        $id = I('id');
        $viewModel = new ViewModel\ReportViewModel();
        $field = [
            'ctime', 'id', 'complaint_type_name', 'user_logo', 'user_id', 'user_phone', 'user_name', 'user_phone', 'user_sex',
            'complaint_user_id', 'complaint_logo', 'complaint_nickname', 'complaint_sex', 'complaint_phone', 'pic'
        ];
        $fb = $viewModel->where(array('id' => $id))->field($field)->find();
        if ($fb) {
            $fb['user_sex_text'] = empty($fb['user_sex']) ? $sex_enum['3'] : $sex_enum[$fb['user_sex']];
            $fb['complaint_sex_text'] = empty($fb['complaint_sex']) ? $sex_enum['3'] : $sex_enum[$fb['complaint_sex']];
            $fb['ctime'] = date('Y-m-d H:i', $fb['ctime']);
            $fb['imgs'] = empty($fb['pic']) ? '' : explode(",", $fb['pic']);
        }
        $viewModel1 = new ViewModel\ReportreplyViewModel();
        $field = [
            'username AS adminname', 'content', 'ctime'
        ];
        $data = $viewModel1->where(array('complaint_id' => $id))->field($field)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $this->assign(array(
            'id' => $id,
            'fb' => $fb,
            'data' => $data,
        ));
        $this->display();
    }

//回复弹框
    public function reportreplyback() {
        $id = I('id');
        $this->id = $id;
        $this->display();
    }

//回复提交
    public function reportreplyfunc() {
        $admin_id = $this->admin_id;
        $id = I('id');
        $content = I('content');
        $viewModel = new ViewModel\ReportViewModel();
        $data = $viewModel->where(array('id' => $id))->find();
        if (!empty($data)) {
            //反馈uid给接口
            $user_id = $data['user_id'];
            $this->feedbackreplysend($id, $user_id, $admin_id, $content); //发送回复消息
        } else {
            $this->ajaxReturn(array('code' => '2', 'result' => '获取数据失败'));
        }
    }

    public function feedbackreplysend($id, $user_id, $admin_id, $content) {
        $app_code = $this->app_code;
        if (empty($user_id) || empty($content)) {
            $this->ajaxReturn(array('code' => '2', 'result' => '回复失败，请重试'));
            exit();
        }
        $appKey = '0vnjpoad0vimz';
        $appSecret = 'yMGaWW6VqHUSb';
        $RongCloud = new \Index\Service\RongCloud($appKey, $appSecret);
        //$arr['operation'] =  'op1';
        //$arr['sourceUserId'] =  '1';
        //$arr['targetUserId'] =  I('uid2');
        $arr['content'] = $content;
        $arr['extra'] = 'helloExtra';
        $string = json_encode($arr);
        $result = $RongCloud->Message()->publishPrivate('54', $user_id, 'RC:TxtMsg', $string, '', '', '', '0', '1', '1');
        $result = json_decode($result, true);
        if ($result['code'] == '200') {
            $data['admin_id'] = $admin_id;
            $data['complaint_id'] = $id;
            $data['complaint_user_uid'] = $user_id;
            $data['content'] = $content;
            $data['ctime'] = time();
            $data['app_flag'] = $app_code;
            $chat_feedback_re = M('chat_complaint_re');
            $chat_feedback_re->add($data);
            $this->ajaxReturn(array('code' => '1', 'result' => '回复成功'));
            exit();
        } else {
            $this->ajaxReturn(array('code' => '2', 'result' => '回复失败，请重试'));
            exit();
        }
    }
//删除举报
    public function deletesubmit() {
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_complaint');
            $result = $model->where(array('id' => $id))->delete();
            if ($result) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }
    }

    //举报回复列表
    public function reportbacklist() {
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $map['chat_complaint_re.app_flag'] = $app_code;
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['chat_complaint_re.ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['chat_complaint_re.ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['chat_complaint_re.ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            if (is_numeric($keyword)) {
                $where['chat_complaint_re.complaint_id'] = array('eq', $keyword);
            } else if (is_string($keyword)) {
                $where['chat_admin.username'] = array('like', '%' . $keyword . '%');
                $where['chat_admin.name'] = array('like', '%' . $keyword . '%');
            }
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\ReportreplyViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $field = [
            'id', 'content', 'chat_complaint_re.ctime AS time', 'pic', 'username AS adminname', 'complaint_type_name', 'pic','complaint_id','chat_complaint_re.id AS del_id'
        ];
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order('ctime desc')->select();
//        dump($data);
//        die;
        foreach ($data as $key => $value) {
            $data[$key]['imgs'] = empty($value['pic']) ? '' : explode(",", $value['pic']);
            $data[$key]['admin_name_text'] = $data[$key]['adminname'];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['time']);
        }
        $search = array('keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search
        ));
        $this->display();
    }

    //举报回复列表删除
    public function deletereplysubmit() {
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_complaint_re');
            $result = $model->where(array('id' => $id))->delete();
            if ($result) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }
    }

}
